﻿using System;
using System.Collections.Generic;

namespace ConsoleApplication2
{
    public class Solution
    {
        public int falling_disks(int[] well, int[] disks)
        {
            RQM rqm = new RQM(well);
            int count = 0;
            int maxDepth = well.Length - 1;

            foreach (var disk in disks)
            {
                Node minRing = rqm.QueryMin(0, maxDepth);

                if (minRing.Value < disk)
                {
                    maxDepth = minRing.Index;
                }
                maxDepth--;
                if (maxDepth > -1)
                {
                    count++;
                }
            }

            return count;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var sol = new Solution();
            var A = new int[] { 19, 7, 18, 9, 5, 5, 9, 8, 11, 16, 19, 16 };
            var B = new int[] { 7, 3, 2, 7, 6 };

            //var A = new int[] { 5, 6, 4, 3, 6, 2, 3 };
            //var B = new int[] { 2, 3, 5, 2, 4 };
            int n = sol.falling_disks(A, B);
            System.Console.WriteLine(n);
        }
    }
}
